package com.xh.car.controller;

import com.xh.car.bean.Driver;
import com.xh.car.bean.Userinfo;
import com.xh.car.service.UserinfoService;
import com.xh.car.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author PangJunjie
 * @Date 2024/5/8/008
 */
@Controller
@RequestMapping("/userinfo")
public class UserinfoController {
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("")
    public String index() {
        return "userinfo/userinfo-list";
    }

    @GetMapping("/edit")
    public String edit(Integer id, Model model) {
        if (id != null) {
            model.addAttribute("userinfo", userinfoService.getById(id));
        }
        return "userinfo/userinfo-edit";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Userinfo userinfo, HttpSession session) {
        Userinfo loginInfo = (Userinfo) session.getAttribute("loginInfo");
        if (loginInfo.getRole() == 2) {
            userinfo.setDepartmentId(loginInfo.getDepartmentId());
        }
        return userinfoService.getPage(page, limit, userinfo);
    }

    @PostMapping(value = {"/update", "/status", "/password"})
    @ResponseBody
    public R update(Userinfo userinfo, Driver driver) {
        userinfoService.saveOrUpdate(userinfo, driver);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        userinfoService.delete(id);
        return R.ok();
    }

    @GetMapping("/check/password")
    @ResponseBody
    public R checkPassword(Userinfo userinfo) {
        return userinfoService.checkPassword(userinfo);
    }

    @GetMapping("/password")
    public String password() {
        return "userinfo/userinfo-password";
    }

    @GetMapping("/driver/select")
    @ResponseBody
    public R driverSelect() {
        return userinfoService.getDriverSelect();
    }
}
